package com.zlm.spider.util;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.stereotype.Component;

/**
 * @author Harbour
 * @date 2021-04-12 10:18
 */
@Component
public class HttpClientsUtils {

    private static volatile PoolingHttpClientConnectionManager httpClientsPool = null;

    /**
     * 获取http clients 连接池
     * @return 连接池管理
     */
    public static PoolingHttpClientConnectionManager getHttpClientsPoolManager() {
        if (httpClientsPool == null) {
            synchronized (HttpClientsUtils.class) {
                if (httpClientsPool == null) {
                    httpClientsPool = new PoolingHttpClientConnectionManager();
                    httpClientsPool.setMaxTotal(100); // 最多100个连接
                    httpClientsPool.setDefaultMaxPerRoute(10); // 每个路由最多10个连接
                }
            }
        }
        return httpClientsPool;
    }
}
